cmake_minimum_required(VERSION 3.10)
project(TextProcessor VERSION 1.0)

# 设置C++标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 包含头文件目录
include_directories(${PROJECT_SOURCE_DIR}/include)

# JSON库已静态包含在include/nlohmann目录中

# 使用lib目录中的SQLite库
include_directories(${PROJECT_SOURCE_DIR}/lib/sqlite3/include)
if(WIN32)
    if(CMAKE_BUILD_TYPE STREQUAL "Debug")
        set(SQLITE_LIB_PATH "${PROJECT_SOURCE_DIR}/lib/sqlite3/debug/sqlite3.lib")
        set(SQLITE_DLL_PATH "${PROJECT_SOURCE_DIR}/lib/sqlite3/debug/sqlite3.dll")
    else()
        set(SQLITE_LIB_PATH "${PROJECT_SOURCE_DIR}/lib/sqlite3/release/sqlite3.lib")
        set(SQLITE_DLL_PATH "${PROJECT_SOURCE_DIR}/lib/sqlite3/release/sqlite3.dll")
    endif()
else()
    if(CMAKE_BUILD_TYPE STREQUAL "Debug")
        set(SQLITE_LIB_PATH "${PROJECT_SOURCE_DIR}/lib/sqlite3/debug/libsqlite3.a")
    else()
        set(SQLITE_LIB_PATH "${PROJECT_SOURCE_DIR}/lib/sqlite3/release/libsqlite3.a")
    endif()
endif()

# 如果找不到库文件，打印警告信息但继续构建
if(NOT EXISTS ${SQLITE_LIB_PATH})
    message(WARNING "SQLite3 library not found at ${SQLITE_LIB_PATH}. Using the default path.")
    if(WIN32)
        set(SQLITE_LIB_PATH "${PROJECT_SOURCE_DIR}/lib/sqlite3/release/sqlite3.lib")
        set(SQLITE_DLL_PATH "${PROJECT_SOURCE_DIR}/lib/sqlite3/release/sqlite3.dll")
    else()
        set(SQLITE_LIB_PATH "${PROJECT_SOURCE_DIR}/lib/sqlite3/release/libsqlite3.a")
    endif()
endif()

set(SQLITE_LIBRARIES ${SQLITE_LIB_PATH})

# 添加源文件
file(GLOB SOURCES "src/*.cpp")

# 生成可执行文件
add_executable(TextProcessor ${SOURCES})

# 链接SQLite库
target_link_libraries(TextProcessor ${SQLITE_LIBRARIES})

# 设置输出目录
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)

# 在Windows上，复制SQLite3的DLL到输出目录（如果存在的话）
if(WIN32)
    # 检查是否存在DLL
    if(EXISTS ${SQLITE_DLL_PATH})
        add_custom_command(TARGET TextProcessor POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy_if_different
            ${SQLITE_DLL_PATH}
            ${EXECUTABLE_OUTPUT_PATH}/$<CONFIG>/
        )
        message(STATUS "Will copy SQLite3 DLL from ${SQLITE_DLL_PATH} to output directory.")
    else()
        # 尝试查找其他可能的位置
        file(GLOB SQLITE_DLL_FILES 
            "${PROJECT_SOURCE_DIR}/lib/sqlite3/release/*.dll"
            "${PROJECT_SOURCE_DIR}/lib/sqlite3/debug/*.dll"
            "${PROJECT_SOURCE_DIR}/lib/sqlite3/*.dll"
        )
        
        if(SQLITE_DLL_FILES)
            foreach(DLL_FILE ${SQLITE_DLL_FILES})
                message(STATUS "Found SQLite3 DLL: ${DLL_FILE}")
                add_custom_command(TARGET TextProcessor POST_BUILD
                    COMMAND ${CMAKE_COMMAND} -E copy_if_different
                    ${DLL_FILE}
                    ${EXECUTABLE_OUTPUT_PATH}/$<CONFIG>/
                )
            endforeach()
        else()
            message(WARNING "No SQLite3 DLL found. The program may not run unless sqlite3.dll is in the system path.")
        endif()
    endif()
endif() 